﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;

namespace ShapeDemo
{
    /// <summary>
    /// Shapes与XML存取操作
    /// </summary>
    public class DataIntoXML
    {
        private PointDrawer ptDrawer;
        private RectDrawer rtDrawer;
        private EllipseDrawer elDrawer;
        private PolylineDrawer plDrawer;
        private PolygonDrawer pgnDrawer;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="pt">点</param>
        /// <param name="rt">矩形</param>
        /// <param name="el">圆</param>
        /// <param name="pl">线</param>
        /// <param name="pgn">面</param>
        public DataIntoXML(PointDrawer pt,RectDrawer rt,EllipseDrawer el,PolylineDrawer pl,PolygonDrawer pgn)
        {
            ptDrawer = pt;
            rtDrawer = rt;
            elDrawer = el;
            plDrawer = pl;
            pgnDrawer = pgn;
        }
        
        /// <summary>
        /// 保存到XML文件中
        /// </summary>
        /// <param name="filename">文件位置</param>
        public void Save(string filename)
        {
            string strpolygon = ASPNETX_Adapter.SerializePolygon(pgnDrawer.PolygonCollection);
            string strpoint = ASPNETX_Adapter.SerializePoint(ptDrawer.PointCollection);
            string strpolyline = ASPNETX_Adapter.SerializePolyline(plDrawer.PolylineCollection);
            string strrect = ASPNETX_Adapter.SerializeRect(rtDrawer.RectCollection);
            string strellipse = ASPNETX_Adapter.SerializeEllipse(elDrawer.EllipseCollection);

            StreamWriter swPolygon = new StreamWriter(filename + "polygon.xml");
            swPolygon.Write(strpolygon);
            swPolygon.Dispose();

            StreamWriter swPoint = new StreamWriter(filename + "point.xml");
            swPoint.Write(strpoint);
            swPoint.Dispose();

            StreamWriter swPolyline = new StreamWriter(filename + "polyline.xml");
            swPolyline.Write(strpolyline);
            swPolyline.Dispose();

            StreamWriter swRect = new StreamWriter(filename + "rect.xml");
            swRect.Write(strrect);
            swRect.Dispose();

            StreamWriter swEllipse = new StreamWriter(filename + "ellipse.xml");
            swEllipse.Write(strellipse);
            swEllipse.Dispose();
        }

        /// <summary>
        /// 从XML文件中加载图形
        /// </summary>
        /// <param name="filename">文件位置</param>
        /// <returns></returns>
        public bool Load(string filename)
        {
            bool result=false;
            
            ptDrawer.PointCollection = new List<ASPNETX_WPFPoint>();
            rtDrawer.RectCollection = new List<ASPNETX_WPFRect>();
            elDrawer.EllipseCollection = new List<ASPNETX_WPFEllipse>();
            plDrawer.PolylineCollection = new List<ASPNETX_WPFPolyline>();
            pgnDrawer.PolygonCollection = new List<ASPNETX_WPFPolygon>();

            ptDrawer.PointCanvas.Children.Clear();
            rtDrawer.RectCanvas.Children.Clear();
            elDrawer.EllipseCanvas.Children.Clear();
            plDrawer.PolylineCanvas.Children.Clear();
            pgnDrawer.PolygonCanvas.Children.Clear();


            FileInfo textFile;
            StreamReader sr;

            try
            {
                //----Polygon
                textFile = new FileInfo(filename + "polygon.xml");
                sr = textFile.OpenText();

                pgnDrawer.PolygonCollection = ASPNETX_Adapter.DeSerializePolygon(sr.ReadToEnd());

                sr.Dispose();

                foreach (ASPNETX_WPFPolygon item in pgnDrawer.PolygonCollection)
                {
                    pgnDrawer.PolygonCanvas.Children.Add(item.PolygonBody);
                }

                //----Point
                textFile = new FileInfo(filename + "point.xml");
                sr = textFile.OpenText();

                ptDrawer.PointCollection = ASPNETX_Adapter.DeSerializePoint(sr.ReadToEnd());

                sr.Dispose();

                foreach (ASPNETX_WPFPoint item in ptDrawer.PointCollection)
                {
                    ptDrawer.PointCanvas.Children.Add(item.DrawPoint());
                }

                //----Polyline
                textFile = new FileInfo(filename + "polyline.xml");
                sr = textFile.OpenText();

                plDrawer.PolylineCollection = ASPNETX_Adapter.DeSerializePolyline(sr.ReadToEnd());

                sr.Dispose();

                foreach (ASPNETX_WPFPolyline item in plDrawer.PolylineCollection)
                {
                    plDrawer.PolylineCanvas.Children.Add(item.PolylineBody);
                }

                //----Rect
                textFile = new FileInfo(filename + "rect.xml");
                sr = textFile.OpenText();

                rtDrawer.RectCollection = ASPNETX_Adapter.DeSerializeRect(sr.ReadToEnd());

                sr.Dispose();

                foreach (ASPNETX_WPFRect item in rtDrawer.RectCollection)
                {
                    rtDrawer.RectCanvas.Children.Add(item.RectBody);
                }

                //----Ellipse
                textFile = new FileInfo(filename + "ellipse.xml");
                sr = textFile.OpenText();

                elDrawer.EllipseCollection = ASPNETX_Adapter.DeSerializeEllipse(sr.ReadToEnd());

                sr.Dispose();

                foreach (ASPNETX_WPFEllipse item in elDrawer.EllipseCollection)
                {
                    elDrawer.EllipseCanvas.Children.Add(item.EllipseBody);
                }

                result=true;
            }
            catch
            {
                
            }

            return result;
        }
    }

    /// <summary>
    /// 序列化操作相关方法
    /// </summary>
    public class SimpleSerializer
    {
        /// <summary>
        /// 序列化对象
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="t">对象</param>
        /// <returns></returns>
        public static string Serialize<T>(T t)
        {
            using (StringWriter sw = new StringWriter())
            {
                XmlSerializer xz = new XmlSerializer(t.GetType());
                xz.Serialize(sw, t);
                return sw.ToString();
            }
        }

        /// <summary>
        /// 反序列化为对象
        /// </summary>
        /// <param name="type">对象类型</param>
        /// <param name="s">对象序列化后的Xml字符串</param>
        /// <returns></returns>
        public static object Deserialize(Type type, string s)
        {
            using (StringReader sr = new StringReader(s))
            {
                XmlSerializer xz = new XmlSerializer(type);
                return xz.Deserialize(sr);
            }
        }
    }
}
